//引出全局变量window.systemStateHandle
$(function() {
	platformId = 0;
	errCount = 15;//获取状态错误次数
	getPlatformId(); // 拿到平台ID
	fan1Gauge = null;//风扇转速1
	fan2Gauge = null;//风扇转速2
	temp1Gauge = null;//风扇温度1
	temp2Gauge = null;//风扇温度2
	initGauges();//初始化仪表
	fengshanInit();//风扇档位
	initKaiGuan();//lkj和astk开关
});

function initGauges() {
	fan1Gauge = new RadialGauge({
		renderTo: 'fan-1',
		width: 100,
		height: 100,
		units: 'KPS',
		minValue: 0,
		maxValue: 3,
		majorTicks: ['0', '0.5', '1', '1.5', '2', '2.5', '3'],
		minorTicks: '2',
		strokeTicks: true,
		highlights: [],
		colorPlate: "#2C2F33",
		colorValueText: "#FFFFFF",
		colorNumbers: "#FFFFFF",
		colorMajorTicks: "#FFFFFF",
		colorMinorTicks: "#FFFFFF",
		colorStrokeTicks: "#FFFFFF",
		colorBarStroke: "#FFFFFF",
		borderShadowWidth: 0,
		borders: false,
		needleType: "arrow",
		needleWidth: 2,
		needleCircleSize: 7,
		needleCircleOuter: true,
		needleCircleInner: false,
		animationDuration: 500,
		animationRule: "linear",
		valueBoxStroke: 0,
		valueInt: 1,
		valueDec: 3,
		colorValueBoxBackground: "#2C2F33",
	}).draw();

	fan2Gauge = new RadialGauge({
		renderTo: 'fan-2',
		width: 100,
		height: 100,
		units: 'KPS',
		minValue: 0,
		maxValue: 3,
		majorTicks: ['0', '0.5', '1', '1.5', '2', '2.5', '3'],
		minorTicks: '2',
		strokeTicks: true,
		highlights: [],
		colorPlate: "#2C2F33",
		colorValueText: "#FFFFFF",
		colorNumbers: "#FFFFFF",
		colorMajorTicks: "#FFFFFF",
		colorMinorTicks: "#FFFFFF",
		colorStrokeTicks: "#FFFFFF",
		colorBarStroke: "#FFFFFF",
		borderShadowWidth: 0,
		borders: false,
		needleType: "arrow",
		needleWidth: 2,
		needleCircleSize: 7,
		needleCircleOuter: true,
		needleCircleInner: false,
		animationDuration: 500,
		animationRule: "linear",
		valueBoxStroke: 0,
		valueInt: 1,
		valueDec: 3,
		colorValueBoxBackground: "#2C2F33",
	}).draw();
	temp1Gauge = new LinearGauge({
		renderTo: 'temp-1',
		type: "linear-gauge",
		width: 75,
		height: 150,
		borders: false,
		barBeginCircle: false,
		highlights: false,
		colorPlate: "#2C2F33",
		colorBorderShadow: "#2C2F33",
		colorValueText: "#FFFFFF",
		colorNumbers: "#FFFFFF",
		colorMajorTicks: "#FFFFFF",
		colorMinorTicks: "#FFFFFF",
		colorStrokeTicks: "#FFFFFF",
		colorBarStroke: "#FFFFFF",
		colorBar: "#6E6E6E",
		colorBarProgress: "#565656",
		fontNumbersSize: 10,
		fontUnitsSize: 10,
		fontValueSize: 10,
		minorTicks: 2,
		value: 0,
		majorTicks: ['0', '20', '40', '60', '80', '100'],
		animationRule: "bounce",
		animationDuration: 750,
		barWidth: 20,
		barStrokeWidth: 1,
		valueVoxBorderRadius: 0,
		valueBox: false,
		valueTextShadow: false,
	}).draw();
	temp2Gauge = new LinearGauge({
		renderTo: 'temp-2',
		type: "linear-gauge",
		width: 75,
		height: 150,
		borders: false,
		barBeginCircle: false,
		highlights: false,
		colorPlate: "#2C2F33",
		colorBorderShadow: "#2C2F33",
		colorValueText: "#FFFFFF",
		colorNumbers: "#FFFFFF",
		colorMajorTicks: "#FFFFFF",
		colorMinorTicks: "#FFFFFF",
		colorStrokeTicks: "#FFFFFF",
		colorBarStroke: "#FFFFFF",
		colorBar: "#6E6E6E",
		colorBarProgress: "#565656",
		fontNumbersSize: 10,
		fontUnitsSize: 10,
		fontValueSize: 10,
		minorTicks: 2,
		value: 0,
		majorTicks: ['0', '20', '40', '60', '80', '100'],
		animationRule: "bounce",
		animationDuration: 750,
		barWidth: 20,
		barStrokeWidth: 1,
		valueVoxBorderRadius: 0,
		valueBox: false,
		valueTextShadow: false,
	}).draw();
}
//根据返回的值更新显示按钮的颜色
function switchStatus(wrapper, status) {
	switch (status) {
		case 0:
			wrapper.css({
				background: 'grey'
			});
			break;
		case 1:
			wrapper.css({
				background: 'green'
			});
			break;
		case 2:
			wrapper.css({
				background: 'red'
			});
			break;
		default:
			wrapper.css({
				background: 'grey'
			});
			break;
	}
}

//根据平台id获取平台监测状态信息
function startUpdateState(platformId) {
	window.systemStateHandle = setInterval(function() {
		$.ajax({
			"url": "../../../manualTest/getPlatformStatus",
			"type": "post",
			"data": {
				"platformId": platformId
			},
			"success": function(result) {
				if (result.flag == 0) {
					var statusInfo = result.content;
					var ckUnitInfo = statusInfo.ckUnit;
					var xhtlUnitInfo = statusInfo.xhtlUnit;
					//电源状态
					switchStatus($('#system-status-container-power-light'), statusInfo.dy);
					//主机单元状态
					switchStatus($('#system-status-container-main-master-item-can-light'), statusInfo.zj_canzt);
					switchStatus($('#system-status-container-main-master-item-rs-light'), statusInfo.zj_ckzt);
					//测试单元状态
					switchStatus($('#astsStatuDiv .unit-slot:nth(0) .unit-slot-light'), ckUnitInfo.tx_1);
					switchStatus($('#astsStatuDiv .unit-slot:nth(1) .unit-slot-light'), ckUnitInfo.tx_2);
					switchStatus($('#astsStatuDiv .unit-slot:nth(2) .unit-slot-light'), ckUnitInfo.tx_3);
					switchStatus($('#astsStatuDiv .unit-slot:nth(3) .unit-slot-light'), ckUnitInfo.tx_4);
					switchStatus($('#astsStatuDiv .unit-slot:nth(4) .unit-slot-light'), ckUnitInfo.zk);
					switchStatus($('#astsStatuDiv .unit-slot:nth(5) .unit-slot-light'), ckUnitInfo.dyzt);
					//信号状态
					switchStatus($('#astsStatuDiv .unit-slot:nth(6) .unit-slot-light'), xhtlUnitInfo.dy_1);
					switchStatus($('#astsStatuDiv .unit-slot:nth(7) .unit-slot-light'), xhtlUnitInfo.dy_2);
					switchStatus($('#astsStatuDiv .unit-slot:nth(8) .unit-slot-light'), xhtlUnitInfo.cksc_A);
					switchStatus($('#astsStatuDiv .unit-slot:nth(9) .unit-slot-light'), xhtlUnitInfo.cksc_B);
					switchStatus($('#astsStatuDiv .unit-slot:nth(10) .unit-slot-light'), xhtlUnitInfo.cksc_C);
					switchStatus($('#astsStatuDiv .unit-slot:nth(11) .unit-slot-light'), xhtlUnitInfo.cksc_D);
					switchStatus($('#astsStatuDiv .unit-slot:nth(12) .unit-slot-light'), xhtlUnitInfo.szsr_A);
					switchStatus($('#astsStatuDiv .unit-slot:nth(13) .unit-slot-light'), xhtlUnitInfo.szsr_B);
					switchStatus($('#astsStatuDiv .unit-slot:nth(14) .unit-slot-light'), xhtlUnitInfo.szsr_C);
					switchStatus($('#astsStatuDiv .unit-slot:nth(15) .unit-slot-light'), xhtlUnitInfo.wsfsc);
					switchStatus($('#astsStatuDiv .unit-slot:nth(16) .unit-slot-light'), xhtlUnitInfo.mn_1);
					switchStatus($('#astsStatuDiv .unit-slot:nth(17) .unit-slot-light'), xhtlUnitInfo.mn_2A);
					switchStatus($('#astsStatuDiv .unit-slot:nth(18) .unit-slot-light'), xhtlUnitInfo.mn_2B);
					switchStatus($('#astsStatuDiv .unit-slot:nth(19) .unit-slot-light'), xhtlUnitInfo.jg);
					switchStatus($('#astsStatuDiv .unit-slot:nth(20) .unit-slot-light'), xhtlUnitInfo.jx);
					switchStatus($('#astsStatuDiv .unit-slot:nth(21) .unit-slot-light'), xhtlUnitInfo.gl);
					//测试状态，数据库连接状态，服务器连接状态
					switchStatus($('#platform-test-status-light'), statusInfo.cszt);
					switchStatus($('#platform-db-status-light'), statusInfo.sjkzt);
					switchStatus($('#platform-connect-status-light'), statusInfo.fwqzt);
					//内存和CPU使用情况
					$('#server-memmery-status-gauge').css('width', statusInfo.nc / 4096.0 * $('.bar-container').width());
					$('#server-memmery-status-gauge').text(statusInfo.nc / 1024.0 + 'G');
					$('#server-cpu-status-gauge').css('width', statusInfo.cpu / 100.0 * $('.bar-container').width());
					$('#server-cpu-status-gauge').text(statusInfo.cpu + '%');
					if (fan1Gauge != null) {
						fan1Gauge.value = statusInfo.fszs_1 / 1000.0;
					}
					if (fan1Gauge != null) {
						fan2Gauge.value = statusInfo.fszs_2 / 1000.0;
					}
					if (temp1Gauge != null) {
						temp1Gauge.value = statusInfo.fswd_1;
					}
					if (temp2Gauge != null) {
						temp2Gauge.value = statusInfo.fswd_2;
					}

				} else {
					errCount++;
					if (errCount >= 30) {
						errCount = 0;
						$("#linkStatus").css("background", "red");
						var content = "<div style='color:black;'>" + result.errorInfo + "</div>";
						warning(content);
					}
				}
			},
			"error": function() {
				errCount++;
				if (errCount >= 30) {
					errCount = 0;
					var content = "<div style='color:black;'>获取系统状态失败！</div>";
					danger(content);
				}

			}
		});
	}, 800);
}

function getPlatformId() { // 从测试序列界面的链接路径中截取ID
	var url = location.search; // 获取url中"?"符后的字串
	path = location.host;
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		platformId = strs[3].split("=")[1];
		startUpdateState(platformId);
	}
}
//风扇档位
function fengshanInit(){
	$("#ex8").slider({
		tooltip: ''
	});
	$("#ex9").slider({
		tooltip: ''
	});
	dangweiSendBool = false;//如果点击了一次换挡，那么只有等返回数据后，才可以点击下一次
	currentFSBool0 = false;
	currentFSBool1 = false;
	$("#ex8").on("slide", function(slideEvt) {
		if(dangweiSendBool){
			return;
		}
		dangweiSendBool = false;
		if(!currentFSBool0){
			return;
		}
		addLoading();
		currentFSBool0 = false;
		$('body').css('pointer-events','none');
		$.ajax({
			type : "POST",
			url : "../../../manualTest/setFanSpeed",
			dataType : "json",
			data : {
				"platformId" : platformId,
				"index":1,
				"gear":slideEvt.value
			},
			success : function(data) {
				removeLoading();
				$('body').css('pointer-events','auto');
				console.log(data);
				if (data.flag == "0") {
					var info = "调整风扇转速操作成功!";
					book(info);
				} else {
					var info = data.errorInfo;
					book(info);
				}
			},error:function(data){
				removeLoading();
				console.log(data);
				book('调整风扇转速失败');
			}
		});
	});
	$("#ex9").on("slide", function(slideEvt) {
		if(dangweiSendBool){
			return;
		}
		dangweiSendBool = false;
		if(!currentFSBool1){
			return;
		}
		addLoading();
		currentFSBool1 = false;
		$('body').css('pointer-events','none');
		$.ajax({
			type : "POST",
			url : "../../../manualTest/setFanSpeed",
			dataType : "json",
			data : {
				"platformId" : platformId,
				"index":2,
				"gear":slideEvt.value
			},
			success : function(data) {
				removeLoading();
				$('body').css('pointer-events','auto');
				dangweiSendBool = false;
				if (data.flag == "0") {
					var info = "调整风扇转速操作成功!";
					book(info);
				} else {
					var info = data.errorInfo;
					book(info);
				}
			},error:function(data){
				removeLoading();
				console.log(data);
				book('调整风扇转速失败');
			}
		});
	});
}

//判断当前是点击的哪个风扇
function dangweiFun(index,value)
{
	if(index == 1){
		currentFSBool0 = true;
	}else{
		currentFSBool1 = true;
	}
}
//LKJ和ASTS的开关
function initKaiGuan()
{
	var kaiGuanSend = false;//当开关按钮点击了一次，等返回数据后，才可以点击下一次
	var kaiOrGuanBool0 = true;
	var kaiOrGuanBool1 = true;
	//LKJ的开关
    $('#kaiguan0').click(function(){
    	if(kaiGuanSend){
    		return;
    	}
    	kaiGuanSend = true;
    	var voltage = 0;
    	kaiOrGuanBool0 = !kaiOrGuanBool0;
    	if(kaiOrGuanBool0){
    		voltage = $('#kaiguanSelect').val();
    		$('#kaiguan0').css('background-image','url(../../styles/css/images/2.png)');
    	}else{
    		voltage = 0;
    		$('#kaiguan0').css('background-image','url(../../styles/css/images/1.png)');
    	}
    	addLoading();
    	$.ajax({
			type : "POST",
			url : "../../../manualTest/setLKJVoltage",
			dataType : "json",
			data : {
				"platformId" : platformId,
				"voltage":voltage
			},
			success : function(data) {
				removeLoading();
				kaiGuanSend = false;
				console.log(data);
				if (data.flag == "0") {
					var info = "";
					if(kaiOrGuanBool0){
						info = "LKJ开启成功,电压："+voltage+"V!";
					}else{
						info = "LKJ关闭成功!";
					}
					book(info);
				} else {
					var info = data.errorInfo;
					book(info);
				}
			},
			error:function(data) {
				removeLoading();
				if(kaiOrGuanBool0){
					info = "LKJ开启失败！";
				}else{
					info = "LKJ关闭失败!";
				}
				book(info);
			}
		});
    });
    //ASTS的开关
    $('#kaiguan1').click(function(){
    	if(kaiGuanSend){
    		return;
    	}
    	kaiGuanSend = true;
    	kaiOrGuanBool1 = !kaiOrGuanBool1;
    	var voltage = 0;
    	if(kaiOrGuanBool1){
    		voltage = 1;
    		$('#kaiguan1').css('background-image','url(../../styles/css/images/2.png)');
    	}else{
    		voltage = 0;
    		$('#kaiguan1').css('background-image','url(../../styles/css/images/1.png)');
    	}
    	addLoading();
    	$.ajax({
			type : "POST",
			url : "../../../manualTest/setASTSVoltage",
			dataType : "json",
			data : {
				"platformId" : platformId,
				"voltage":voltage
			},
			success : function(data) {
				kaiGuanSend = false;
				console.log(data);
				removeLoading();
				if (data.flag == "0") {
					var info = "";
					if(kaiOrGuanBool1){
						info = "ASTS仿真平台开启成功!";
					}else{
						info = "ASTS仿真平台关闭成功!";
					}
					book(info);
				} else {
					var info = data.errorInfo;
					book(info);
				}
			},error:function(data){
				removeLoading();
				if(kaiOrGuanBool1){
					info = "ASTS仿真平台开启失败!";
				}else{
					info = "ASTS仿真平台关闭失败!";
				}
				book(info);
			}
		});
    });
}
